package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.bean.RegisterVerificationcodeBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.MD5Builder;
import com.yitongmao.util.SignUtil;
import com.yitongmao.util.ToastUtils;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.yitongmao.yitmfruit.R.id.et_ctivity_login_yaoqingma;

public class ForgetPassWordActivity extends AppCompatActivity {

    @Bind(R.id.et_activity_login_phone)
    EditText etActivityLoginPhone;
    @Bind(R.id.et_activity_login_yanzhengma)
    EditText etActivityLoginYanzhengma;
    @Bind(R.id.et_ctivitylogin_password)
    EditText etCtivityloginPassword;
    @Bind(et_ctivity_login_yaoqingma)
    EditText etCtivityLoginYaoqingma;
    @Bind(R.id.mZhuCeNext)
    Button mZhuCeNext;
    @Bind(R.id.mForgisterPwd)
    Button mForgisterPwd;
    @Bind(R.id.activity_register)
    AutoLinearLayout activityRegister;
    private TextView mGainCode;
    private LinearLayout zhucehuitui;
    private TimeCount time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_forget_pass_word);
        ButterKnife.bind(this);
        time = new TimeCount(60000, 1000);
        initView();
        setOnClick();
    }
    //点击事件
    private void setOnClick() {
        //获取验证码
        mGainCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etActivityLoginPhone.getText().toString().trim();
                sendVerificationcode(phone);
            }
        });

        mForgisterPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etActivityLoginPhone.getText().toString().trim();
                String code = etActivityLoginYanzhengma.getText().toString().trim();
                String pwd = etCtivityloginPassword.getText().toString().trim();
                String pwd1 = etCtivityLoginYaoqingma.getText().toString().trim();
                if (pwd.equals(pwd1)){
                    if (pwd.length()<6||pwd1.length()>20){
                        ToastUtils.showShort(ForgetPassWordActivity.this,"密码格式不正确");
                    }else {
                        ForgetPass(phone,code,pwd1);
                    }
                }else {
                    ToastUtils.showShort(ForgetPassWordActivity.this,"两次输入的密码不一致");
                }
         }
        });
        zhucehuitui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ForgetPassWordActivity.this.finish();
            }
        });
    }
    private void initView() {
        zhucehuitui = (LinearLayout) findViewById(R.id.zhucehuitui);
        TextChange textChange = new TextChange();
        etActivityLoginPhone.addTextChangedListener(textChange);
        etActivityLoginYanzhengma.addTextChangedListener(textChange);
        etCtivityloginPassword.addTextChangedListener(textChange);
        etCtivityLoginYaoqingma.addTextChangedListener(textChange);
        mGainCode = (TextView) findViewById(R.id.mGainCode);
    }
    //监听edittext的状态
    class TextChange implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (etActivityLoginPhone.length() > 0 && etActivityLoginYanzhengma.length() > 0 && etCtivityloginPassword.length() > 0 && etCtivityLoginYaoqingma.length() > 0) {
                 mZhuCeNext.setVisibility(View.GONE);
                 mForgisterPwd.setVisibility(View.VISIBLE);
            }
        }
    }
    //获取验证码
    public void sendVerificationcode(String phone) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("phone", phone);
        params.put("type", "updatepwd");
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.registerUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson", "" + string);
                Gson gson = new Gson();
                RegisterVerificationcodeBean registerVerificationcodeBean = gson.fromJson(string, RegisterVerificationcodeBean.class);
                if (registerVerificationcodeBean.result.state.equals("1")){
                    time.start();
                }else {
                    ToastUtils.showShort(ForgetPassWordActivity.this,""+registerVerificationcodeBean.result.message);
                }
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
    //忘记密码
    public void ForgetPass(String userName,String code,String pwd) {
        MD5Builder md5Builder=new MD5Builder();
        String password = md5Builder.getMD5Str(pwd);
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("userName",userName);
        params.put("pwd",password);
        params.put("code",code);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.setpasswordUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson",""+string);
                Gson gson=new Gson();
                RegisterVerificationcodeBean registerVerificationcodeBean = gson.fromJson(string, RegisterVerificationcodeBean.class);
                if (registerVerificationcodeBean.result.state.equals("1")){
                    ToastUtils.showShort(ForgetPassWordActivity.this,registerVerificationcodeBean.result.message);
                    Intent intent=new Intent(ForgetPassWordActivity.this,LoginActivity.class);
                    startActivity(intent);
                    ForgetPassWordActivity.this.overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
                }
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {// 计时完毕
            mGainCode.setText("获取验证码");
            mGainCode.setClickable(true);
        }
        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            mGainCode.setClickable(false);//防止重复点击
            mGainCode.setText(millisUntilFinished / 1000 + "S");
        }
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
}
